package db

import (
	"com.jiaguyu/model"

	"com.jiaguyu/logger"
	"fmt"
)

func CreateQuestion(question *model.Question) (err error) {
	fmt.Printf("提交问题失败，err%v\n", question)
	//sqlStr :=`insert into question(question_id,caption,content,author_id,category_id)
	//            values(?,?,?,?,?)`
	//_, err = DB.Exec(sqlStr,question.QuestionId,question.Caption,question.Content,question.AuthorId,question.CategoryId)
	//if err != nil {
	//	//日志打印
	//	fmt.Printf("提交问题失败，err%v\n",err)
	//	return
	//}
	//return

	sqlstr := `insert into question(
				 question_id,  caption, content, author_id, category_id)
			   values(?,?,?,?,?)`

	_, err = DB.Exec(sqlstr, question.QuestionId, question.Caption,
		question.Content, question.AuthorId, question.CategoryId)
	if err != nil {
		//logger.Error("create question failed, question:%#v, err:%v", question, err)
		fmt.Printf("提交问题失败，err%v\n", err)
		return
	}

	return
}

//获取问题列表
func GetQuestionList(categoryId int64) (questionList []*model.Question, err error) {
	sqlStr := `select 
                      question_id,caption,content,author_id,category_id,create_time
                from question where category_id=?`
	err = DB.Select(&questionList, sqlStr, categoryId)
	if err != nil {
		logger.Error("问题列表查询失败err:%v\n", err)
		return
	}
	return
}

func GetQuestion(questionId int64) (question *model.Question, err error) {
	//Vdao\db\question.go:52:12: type model.Question is not an expression  init
	question = &model.Question{}
	sqlStr := `select 
                    question_id,caption,content,author_id,category_id,create_time
                    from question where question_id=?`
	err = DB.Get(question, sqlStr, questionId)
	if err != nil {
		logger.Error("get question  failed, err:%v", err)
		return
	}
	return
}
